package com.sky.module.system.post.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.module.suppot.datatrace.util.ContentUtil;
import com.sky.module.system.datatrace.domain.entity.DataTracerEntity;
import com.sky.module.system.datatrace.service.DataTraceService;
import com.sky.module.system.post.dao.PostMapper;
import com.sky.module.system.post.domain.entity.Post;
import com.sky.module.system.post.domain.vo.PostQueryVo;
import com.sky.module.system.post.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author
 * @date 2023/2/27 23:22
 **/
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {


    @Autowired
    private PostMapper postMapper;

    @Autowired
    private DataTraceService dataTraceService;

    @Override
    public Page<Post> list(PostQueryVo postQueryVo) {
        final Page<Post> page = new Page<>(postQueryVo.getPageNo(), postQueryVo.getPageSize());
        LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(postQueryVo.getName()), Post::getName, postQueryVo.getName());
        wrapper.like(StringUtils.isNotBlank(postQueryVo.getPostCode()), Post::getPostCode, postQueryVo.getPostCode());
        wrapper.eq(Post::getStatus, postQueryVo.getStatus());
        final Page<Post> resultPage = postMapper.selectPage(page, wrapper);
        return resultPage;
    }

    /***
     * @description 记录数据变更记录
     * @param post
     * @return boolean
     * @date 2023/3/6 0:31
     *
     */
    @Override
    public boolean update(Post post) {
        Long id = post.getId();
        Post post1 = postMapper.selectById(id);
        baseMapper.updateById(post);

        Map<String, String> map = ContentUtil.getContent(post, post1);
        String oldStr = map.get("oldStr");
        String newStr = map.get("newStr");

        String tableName = ContentUtil.getTableName(post);
//         收集数据变更记录
        DataTracerEntity dataTracerEntity = DataTracerEntity.builder()
                .content("修改岗位信息")
                .createTime(LocalDateTime.now())
                .recordId(post.getId())
                .updateTime(LocalDateTime.now())
                .diffOld(oldStr)
                .diffNew(newStr)
                .tableName(tableName)
                .build();
        dataTraceService.addTrace(dataTracerEntity);
//        dataTraceService.update()
        return false;
    }

//    public void uopdate()
}
